package com.mcb.stream.controller;

import com.mcb.stream.bean.User;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;


public class Demo01 {
    public static void main(String[]args){
         myNoneMatch();
    }


    //noneMath 检查是否没有匹配所有元素，方法参数为断言型接口
    public static void myNoneMatch(){
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        boolean result = numbers.stream().noneMatch(x -> x > 10);
        System.out.println(result);
    }


    //流与原始类型流的转换
    public static void myInterStream(){
        IntStream intStream = IntStream.of(1, 2, 3);
        //原始流 --->  流
        Stream<Integer> stream = intStream.boxed();
        //流  --->   原始流
        IntStream intStream1 = stream.mapToInt(Integer::intValue);
        intStream1.forEach(System.out::println);
    }

    //使用summaryStatistics方法获得stream中元素的各种汇总数据
    public static void mySummaryStatistics(){
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        IntSummaryStatistics stats = numbers.stream().mapToInt(x -> x).summaryStatistics();
        System.out.println("List中最大的数字 : " + stats.getMax());
        System.out.println("List中最小的数字 : " + stats.getMin());
        System.out.println("所有数字的总和   : " + stats.getSum());
        System.out.println("所有数字的平均值 : " + stats.getAverage());
    }

    public static void myMaxAndMin(){
        String[] strings = new String[]{"b", "ab", "abc", "abcd", "abcde"};
        Arrays.stream(strings).max(Comparator.comparing(String::length)).map(x->"最大值："+x).ifPresent(System.out::println);
        Arrays.stream(strings).min(Comparator.comparing(String::length)).map(x->"最小值："+x).ifPresent(System.out::println);
    }

    public static void myPartitioningBy(){
        List<User> users = Arrays.asList(new User("张三", 19, 1000),
                new User("张三", 58, 2000),
                new User("李四", 38, 3000),
                new User("赵五", 48, 4000)
        );
        Map<Boolean, List<User>> collect = users.stream().collect(Collectors.partitioningBy(x -> x.getAge() > 38));
        //将数据根据匹配与否存入两个数组中，true、false
        System.out.println(collect.get(true));
    }

    //分组,相同归纳为一组
    public static void myGtoupingBy(){
        List<User> users = Arrays.asList(new User("张三", 19, 1000),
                new User("张三", 58, 2000),
                new User("李四", 38, 3000),
                new User("赵五", 48, 4000)
        );
        Map<String, List<User>> collect = users.stream().collect(Collectors.groupingBy(x -> x. getName()));
        System.out.println(collect);
    }

    //归约，可以将流中的元素反复结合起来，得到一个值
    public static void myReduce(){
        Optional<Integer> optional = Stream.of(1, 2, 3,4).filter(x -> x > 1).reduce((x, y) -> x + y);
        System.out.println(optional.get());
    }

    public static void myConcat(){
        // [1,3,5]
        Stream<Integer> stream1 = Stream.iterate(1, x -> x + 2).limit(3);
        // [3,5,7]
        Stream<Integer> stream2 = Stream.iterate(1, x -> x + 2).skip(1).limit(3);

        Stream.concat(stream1,stream2).distinct().forEach(System.out::print);
    }

    //需求：先按照首字母排序，之后按照string的长度排序
/*
    public static void myThenComparing(){
        String[] arr1 = {"abc", "a", "bc", "abcd"};
        Arrays.stream(arr1).sorted(Comparator.comparing(this::firstChar).thenComparing(String::length)).forEach(System.out::println);
    }
*/

    public static void mySorted(){
        String[] arr1 = {"abc", "a", "bc", "abcd"};

        System.out.println("-----lambda表达式----");

        Arrays.stream(arr1).sorted((x,y) ->{
            if (x.length()>y.length())
                return -1;
            else if (x.length()<y.length())
                return 1;
            else
                return 0;
        }).forEach(System.out::println);


        System.out.println("-------方法应用----");
        Arrays.stream(arr1).sorted(Comparator.comparing(String::length)).forEach(System.out::println);
    }

    //拆解流，将流中每一个元素拆解成一个流
    public static void myFlapMap(){
        String[] arr1 = {"a", "b"};
        String[] arr2 = {"e", "f"};
        String[] arr3 = {"h", "j"};

        Stream.of(arr1,arr2,arr3).flatMap(Arrays::stream).forEach(System.out::println);
    }

    public static void myToMap(){
        List<User> users = Arrays.asList(
                new User("张三", 20, 1000),
                new User("张三", 58, 2000),
                new User("李四", 38, 3000),
                new User("赵五", 48, 4000)
        );

        //key值不能重复
        //(oldValue,newValue)->newValue)))  出现重复时，存放最后一次的value,此处可以根据需求自行处理；
        Map<String,Integer> collect = users.stream().collect(Collectors.toMap(x -> x.getName(), x -> x.getAge(),(k,v)->v));

        System.out.println(collect);
    }
}
